﻿using BLL;
using DAL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace _10._16HuiYuanWebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HuiyuanController : ControllerBase
    {
        public HuiyuanController (HUiyuanBLL hUiyuanBLL)
        {
            HUiyuanBLL = hUiyuanBLL;
        }

        public HUiyuanBLL HUiyuanBLL { get; }

        /// <summary>
        /// 添加
        /// </summary>
        /// 
        [HttpPost]
        public int Add(Huiyuan huiyuan)
        {
            return HUiyuanBLL.Add(huiyuan);
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public int Login(string username, string password)
        {
            return HUiyuanBLL.Login(username, password);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet]
        public List<Huiyuan> GetHuiyuans()
        {
            return HUiyuanBLL.GetHuiyuans();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public int Delete(int id)
        {

            return HUiyuanBLL.Delete(id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="huiyuan"></param>
        /// <returns></returns>
        /// 
        [HttpPut]
        public int Update(Huiyuan huiyuan)
        {

            return HUiyuanBLL.Update(huiyuan);
        }
        /// <summary>
        /// 反添
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public Huiyuan GetHuiyuan(int id)
        {
            return HUiyuanBLL.GetHuiyuan(id);
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public IActionResult Pic(IFormFile file)
        {
            string newFile = Directory.GetCurrentDirectory() + @"\wwwroot\Pic\" + file.FileName;
            using (FileStream fs = new FileStream(newFile, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            //输出文件路径
            return Ok("http://localhost:5145/Pic/" + file.FileName);
        }
    }
}
